/* 
 * File:   AllParametersCommand.cpp
 * Author: Gab'z
 * 
 * Created on 13 juin 2012, 18:47
 */

#include "AllParametersCommand.h"
#include "../../Context.h"
#include " ../../../../memento/ParametersMemento.h"

namespace calculator {

    AllParametersCommand::AllParametersCommand(const Parameters* parameters) : ParametersCommand("SETTINGS"), _parameters(parameters->clone()) {
    }

    AllParametersCommand::AllParametersCommand(const std::string name) : ParametersCommand(name) {
    }

    AllParametersCommand::AllParametersCommand(const AllParametersCommand& orig) : ParametersCommand(orig._name), _parameters(orig._parameters->clone()) {
    }

    AllParametersCommand::~AllParametersCommand() {
        delete _parameters;
    }

    AllParametersCommand* AllParametersCommand::clone() const {
        // clonage différent des autres
        return new AllParametersCommand(*this);
    }

    void AllParametersCommand::apply(Parameters* parameters) const {
        Context::getInstance()->setParameters(_parameters->clone());
    }

    const Memento* AllParametersCommand::createMemento() const throw (CommandException) {
        return new ParametersMemento(clone(), Context::getInstance()->getParameters()->clone());
    }

    void AllParametersCommand::restoreFromMemento(const Memento* memento) const throw (MementoException) {
        const ParametersMemento* parametersMemento = dynamic_cast<const ParametersMemento*> (memento);
        if (parametersMemento == 0) {
            std::string str = "Bad Memento type, expected : ParametersMemento, was ";
            str += typeid (*memento).name();
            throw MementoException(str);
        }
        Context::getInstance()->setParameters(parametersMemento->_parameters->clone());
    }
}